package com.zyd.controller.login;

import com.aliyuncs.dysmsapi.model.v20170525.QuerySendDetailsResponse;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.zyd.message.MessageSender;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;


import com.zyd.io.vo.res.ResBody;
import com.zyd.constants.ErrorDef;



@RestController
@RequestMapping(value = "/message")
public class MessageSendController {


    @Autowired
    private MessageSender messageSender;
    @Value("${templateCode}")
    private String templateCode;
    @Value("${signName}")
    private String signName;

    /**
     * 消息发送模拟
     *
     * @param
     * @return
     */

    @RequestMapping(value = "/send", method = RequestMethod.POST)
    public Map sendMessage(@RequestBody Map<String,String> map){
        HashMap<String, Object> result = new HashMap<String, Object>();
//        System.out.println(signName);
        map.put("templateCode",templateCode);
        map.put("signName",signName);
        map.put("param","{\"code\":\""+map.get("code")+"\"}");

        //消息发送
        try {
            SendSmsResponse sendSmsResponse = messageSender.sendSms(map.get("mobile"), map.get("signName"), map.get("templateCode"), map.get("param"));
            System.out.println(getClass().getName()+"-----message:"+sendSmsResponse.getMessage());
            System.out.println(getClass().getName()+"-----code:"+sendSmsResponse.getCode());
            String mobile = map.get("mobile");
            String  regex="^((13[0-9])|(14[5,7])|(15[0-3,5-9])|(17[0,3,5-8])|(18[0-9])|166|198|199|(147))\\d{8}$";
            if (StringUtils.isEmpty(mobile) || ! mobile.matches(regex)  ) {
               result.put("State",false);
               result.put("StateCode",500);
               result.put("Message","手机格式不正确");
            }
            else {
                result.put("State", true);
                result.put("StateCode",200);
                result.put("Message","短信发送成功");
            }
        } catch (ClientException e) {
            e.printStackTrace();
            result.put("State",false);
            result.put("StateCode",500);
            result.put("Message","系统异常");
        }
        return result;
    }

    @RequestMapping(value = "/send2", method = RequestMethod.POST)
    public String sendMessage(String bizld,String mobile) {
        try {
            QuerySendDetailsResponse querySendDetailsResponse = messageSender.querySendDetails(bizld, mobile);
            ArrayList<String> strings = new ArrayList<String>();
            strings.add(querySendDetailsResponse.getMessage());
            strings.add(querySendDetailsResponse.getCode());
            System.out.println(strings);
            return strings.toString();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return "";
    }
}
